package supplierconsumer;

import java.util.List;
import java.util.Vector;

public class Container {
	
	List<Dumpling> dumplings = new Vector<>();
	
	// 非静态同步方法锁对象
	public synchronized boolean push(Dumpling d) {
		System.out.println(d.getFilling() + "馅的饺子被添加了");
		boolean result =  dumplings.add(d);
		if (result) {
			// 随机唤醒等待池中的一个线程
			this.notify();
		}
		return result;
	}
	
	public synchronized Dumpling pop() {
		if (dumplings.size() == 0) {
			try {
				// 本线程添加到了等待池，释放锁
				System.out.println(Thread.currentThread().getName() + "进入等待池");
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName() + "被唤醒");
		}
		Dumpling remove = dumplings.remove(0);
		return remove;
	}
}
